import $store from '@/store/index.js';
export default {
	/* 获取会话列表 */
	getSesstion(data){
		return new Promise(async(resolve,reject)=>{
			try{
				let list = await $store.state.user.sqlite.session.order('last_time desc').get()
				resolve(list)
			}catch(e){
				reject(e)
			}
		})
	},
	
	/* 获取某个会话 */
	getSesstionId(chat_id,fields){
		return $store.state.user.sqlite.session.where({ field: 'chat_id', val: `'${chat_id}'` }).fields(fields).first()
	},
	/* 修改会话列表 */
	editSesstion(chat_id,data){
		return new Promise(async(resolve,reject)=>{
			try{
				let editres = await $store.state.user.sqlite.session.where({ field: 'chat_id', val: `'${chat_id}'`}).update(data)
				resolve(editres)
			}catch(e){
				console.log(e);
				reject(e)
			}
		})
	},
	/* 删除会话列表 */
	delSesstion(chat_id){
		return new Promise(async(resolve,reject)=>{
			try{
				let deleteres = await $store.state.user.sqlite.session.where({ field: 'chat_id', val: `'${chat_id}'`}).delete()
				resolve(deleteres)
			}catch(e){
				reject(e)
			}
		})
	},
	/* 新增或替换会话列表 */
	replaceSesstion(data){
		return new Promise(async(resolve,reject)=>{
			try{
				let replaceres = await $store.state.user.sqlite.session.replace(data)
				resolve(replaceres)
			}catch(e){
				reject(e)
			}
		})
	}
}